/*
 * Alex Veltsistas G00651470
 * Josh Hornsby G00721274
 * 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package proj3;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class SellerClient
{
    public static boolean checkCommand(String command)
    {
        switch(command)
        {
            case "login":
            case "list":
            case "sell":
            case "add":
            case "exit":
            	return true;
            default:
                return false;
        }
    }
    public static String formCommand(String[] c)
    {
        String s = "";
        for(int x = 0; x < c.length; x++)
        {
            if(x == c.length - 1)
                s += c[x];
            else
                s += c[x] + " ";
        }
        return s;
    }
    public static void main(String[] args)
    {
        InetAddress ip = null;
        int port = 5454;
        try
        {
            ip = InetAddress.getByName("127.0.0.1");
            //Handle args passing <hostName/ip> <port number>
            //Handle passing in host name
            if(args.length > 0)
            {
                ip = InetAddress.getByName(args[0]);
                //handle passing in port
                if(args.length > 1)
                    port = Integer.parseInt(args[1]);
            }
        }
        catch(UnknownHostException e)
        {
            System.out.println("Failed to connect to Host:" + args[0]);
            System.exit(1);
        }
        Socket tc = null;
        InputStream input = null;
        OutputStream output = null;
        try
        {
            tc = new Socket(ip, port);
            input = tc.getInputStream();
            output = tc.getOutputStream();
            System.out.println("Connected to server.");
        }
        catch(IOException e)
        {
            System.out.println("IOException when creating Socket");
            System.exit(1);
        }
        Scanner scn = new Scanner(System.in);
        ServerListenerThread t = new ServerListenerThread(input);
        t.start();
        String[] a = {""};
        try 
        {
            while(!a[0].equals("exit"))
            {
                a = (scn.nextLine() + " <**>").split(" "); 
                a[0] = a[0].toLowerCase();
                if(a[0].equals("login") && a.length == 3)
                    a = new String[] {a[0], a[1], "s", "<**>"};
                if(checkCommand(a[0]))
                    output.write(formCommand(a).getBytes("UTF-8"));
                else
                    System.out.println("\"" + a[0] + "\" was an invalid command.");
            }
            t.stopThread();
            tc.close();
        }
        catch (IOException ex) { System.out.println("Error"); }
        System.out.println("Disconnected from server!");
    }
}